package com.nari.temperature.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.struts2.ServletActionContext;

public class FavFTPUtil {

	/**
	 * 下载文件
	 * 
	 * @param hostname
	 *            FTP服务器地址
	 * @param port
	 *            FTP服务器端口号
	 * @param username
	 *            FTP登录帐号
	 * @param password
	 *            FTP登录密码
	 * @param pathname
	 *            FTP服务器文件目录
	 * @param filename
	 *            文件名称
	 * @param localpath
	 *            下载后的文件路径
	 * @param str
	 * @return
	 */
	public static InputStream downloadFile(String hostname, int port,
			String username, String password, String pathname, String filename,
			String localpath) {
		InputStream is = null;
		FTPClient ftpClient = new FTPClient();
		try {
			// 连接FTP服务器
			ftpClient.connect(hostname, port);
			// 登录FTP服务器
			ftpClient.login(username, password);
			// 验证FTP服务器是否登录成功
			int replyCode = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(replyCode)) {
				return null;
			}
			ftpClient.setControlEncoding("UTF-8");
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
			conf.setServerLanguageCode("zh");
			// 切换FTP目录
			boolean cwd = ftpClient.changeWorkingDirectory(new String(pathname.getBytes(),"ISO-8859-1"));
			if (cwd) {
				FTPFile[] ftpFiles = ftpClient.listFiles();
				for (FTPFile file : ftpFiles) {
					if (filename.equalsIgnoreCase(file.getName())) {
						is = ftpClient.retrieveFileStream(file.getName());
						System.out.println(is.available());
					}
				}
			}
			ftpClient.logout();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.logout();
				} catch (IOException e) {
				}
			}
		}
		return is;
	}

	public static boolean downloadPNG(String hostname, int port, String username,
			String password, String pathname, String filename, String localpath) {
		boolean flag = true;
//		FileOutputStream fos = null;
		InputStream iStream = null;
		FTPClient ftpClient = new FTPClient();
		try {
			// 连接FTP服务器
			ftpClient.connect(hostname, port);
			// 登录FTP服务器
			ftpClient.login(username, password);
			// 验证FTP服务器是否登录成功
			int replyCode = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(replyCode)) {
				 return false;
			}
			ftpClient.setControlEncoding("UTF-8");
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
			conf.setServerLanguageCode("zh");
			// 切换FTP目录
			boolean cwd = ftpClient.changeWorkingDirectory(new String(pathname.getBytes(),"ISO-8859-1"));
			if (cwd) {
				FTPFile[] ftpFiles = ftpClient.listFiles();
				for (FTPFile file : ftpFiles) {
					if (filename.equalsIgnoreCase(file.getName())) {
						iStream = ftpClient.retrieveFileStream(file.getName());
						BufferedInputStream bis = new BufferedInputStream(iStream);
						// 获取response对象
						HttpServletResponse response = ServletActionContext.getResponse();
						// 设置发送到客户端的响应内容类型
						response.setContentType("img/*");
						response.setCharacterEncoding("utf-8");
						OutputStream out = response.getOutputStream();
						BufferedOutputStream bos = new BufferedOutputStream(out);
						// 通过response写入到浏览器中
						int len = 0;
						byte[] buf = new byte[1024 * 1024 * 8];
						while ((len = bis.read(buf)) != -1) {
							bos.write(buf, 0, len);
						}
						bos.flush();
						bos.close();
						bis.close();
						out.close();
						iStream.close();
					}
				}
			}
			ftpClient.logout();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.logout();
				} catch (IOException e) {
				}
			}
		}
		 return flag;
	}
}